package com.taijia.chapter7;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;

public class ContinerTest {
	
	public static long speedAdd(Collection<String> ls,int times) {
		long timeStart = System.currentTimeMillis();
		for (int i = 0; i < times; i++) {
			ls.add("test" + Math.random() * i);
		}
		return System.currentTimeMillis()-timeStart;
	}
	
	public static long speedReverse(List<String> ls,int times) {
		for (int i = 0; i < times; i++) {
			ls.add("test" + Math.random() * i);
		}
		Collections.shuffle(ls);
		long timeStart = System.currentTimeMillis();
		Collections.reverse(ls);
		return System.currentTimeMillis()-timeStart;
	}
	
	public static void main(String[] args) {
		final int times = 1000000;
		System.out.println(speedAdd(new LinkedList<String>(), times)); // 2050
		System.out.println(speedAdd(new ArrayList<String>(), times)); // 651
		System.out.println(speedAdd(new HashSet<String>(), times)); // 1517
		System.out.println(speedAdd(new TreeSet<String>(), times)); // 2214
		System.out.println("Tangtaijia".hashCode());
		System.out.println(new String("Tangtaijia").hashCode());
		
		final int times2 = 5000000;
		// list reverse test
		System.out.println(speedReverse(new LinkedList<String>(), times2)); // 104
		System.out.println(speedReverse(new ArrayList<String>(), times2)); // 21
	}
}
